package ru.aptu.gsp_banking.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import ru.aptu.gsp_banking.domain.Account;
import ru.aptu.gsp_banking.domain.PublicService;
import ru.aptu.gsp_banking.service.AccountService;
import ru.aptu.gsp_banking.service.PublicServiceService;
import ru.aptu.gsp_banking.snmp.Sender;

import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 2/10/12
 * Time: 9:26 PM
 */

@Controller
public class PublicServiceController {

    @Autowired
    private PublicServiceService publicServiceService;

    @Autowired
    private AccountService accountService;

    private static PublicService currentPublicServiceBill = null;

    private boolean posted = false;

    @ModelAttribute("serviceTypes")
    public List<String> listPublicServiceTypes(Principal principal) {
        return publicServiceService.listPublicServiceTypes();
    }

    @ModelAttribute("accounts")
    public List<Account> listAccounts(Principal principal) {
        final String currentUser = principal.getName();
        return accountService.listAccount(currentUser);
    }

    @RequestMapping(value = "/get_service_bill", method = RequestMethod.POST)
    public String getServiceBillPagePost(Map<String, Object> map) {
        posted = true;
        return "redirect:get_service_bill";
    }

    @RequestMapping(value = "/get_service_bill", method = RequestMethod.GET)
    public String getServiceBillPageGet(Map<String, Object> map) {
        if (!posted) {
            return "redirect:logout";
        }
        posted = false;

        PublicService publicService = new PublicService();
        map.put("publicService", publicService);
        return "get_service_bill";
    }

    @RequestMapping(value = "/public_service", method = RequestMethod.POST)
    public String getServiceBill(@ModelAttribute("publicService") PublicService publicService, BindingResult result) {
        posted = true;

        currentPublicServiceBill = publicServiceService.getBill(publicService);
        if (currentPublicServiceBill == null)
            return "redirect:/no_bills";
        else
            return "redirect:/public_service";
    }

    @RequestMapping(value = "/public_service", method = RequestMethod.GET)
    public String publicService(Map<String, Object> map) {
        if (!posted) {
            return "redirect:logout";
        }
        posted = false;

        if (currentPublicServiceBill == null) {
            posted = true;
            return "redirect:/get_service_bill";
        }

        map.put("publicService", currentPublicServiceBill);
        return "public_service";
    }

    @RequestMapping(value = "/submit_public_service", method = RequestMethod.POST)
    public String submitPublicService(@ModelAttribute("publicService") PublicService publicService, BindingResult result) {
        currentPublicServiceBill.setAccountId(publicService.getAccountId());
        currentPublicServiceBill.setNewValue(publicService.getNewValue());

        boolean publicServiceResult = publicServiceService.submitPublicService(currentPublicServiceBill);
        currentPublicServiceBill = null;
        if (publicServiceResult) {
            Sender.sendV2cTrap("Successfully submitted public service");
            return "redirect:/request_success";
        }
        else {
            Sender.sendV2cTrap("Failed when submitting public service");
            return "redirect:/request_fail";
        }
    }

    @RequestMapping("/no_bills")
    public String noBills() {
        if (!posted) {
            return "redirect:logout";
        }
        posted = false;

        return "no_bills";
    }
}
